package web;

import java.util.Date;
import java.util.List;

import tool.DateUtils;
import vo.Sentence;
import vo.SentenceType;
import business.PaperService;
import business.SentenceService;
import business.SentenceTypeService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class SentenceAction implements Preparable {
	private SentenceService service;
	private PaperService paperService;
	private SentenceTypeService sentenceTypeService;

	private List<Sentence> sentences;
	private List<SentenceType> sentenceTypes;

	// 添加sentence
	private int paperId;
	private String keyword;// sentence 检索参数
	private int sentenceTypeId;// sentence检索参数
	private String text;

	// 分页的变量
	private int curPage;
	private int totalPage;
	private int pageSize = 10;
	private int totalCount;

	// 显示细节
	private String paperTitle;

	public String listSentence() {
		// load句子类别
		System.out.println("sentence type,keyword:" + this.sentenceTypes + "," + this.keyword);
		this.sentenceTypes = sentenceTypeService.getAll();
		if (curPage == 0)
			curPage = 1;

		// 没有关键字，没有类别
		if (keyword == null && sentenceTypeId == 0) {
			totalCount = (int) service.getTotalCount();
			this.sentences = service.getAll((curPage - 1) * pageSize, pageSize);
		}
		// 有关键字，没有类别
		else if (keyword != null && sentenceTypeId == 0) {
			totalCount = (int) service.getTotalCountByKeyword("%"+keyword+"%");
			this.sentences = service.getByKeyword((curPage - 1) * pageSize, pageSize, "%"+keyword+"%");
		}// 没哟关键字，有类别
		else if (keyword == null && sentenceTypeId != 0) {
			totalCount = (int) service.getTotalCountByType(sentenceTypeId);
			this.sentences = service.getByType((curPage - 1) * pageSize, pageSize, sentenceTypeId);
		}// 有关键字和类别
		else if (keyword != null && sentenceTypeId != 0) {
			totalCount = (int) service.getTotalCountByTypeKeyword(sentenceTypeId, "%"+keyword+"%");
			this.sentences = service.getByTypeKeyword((curPage - 1) * pageSize, pageSize, sentenceTypeId, "%"+keyword+"%");
		}

		// 计算总页数
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("sentence count:" + this.sentences.size());

		return Action.SUCCESS;
	}

	public String insertSentence() {
		System.out.println("paperId:" + paperId);
		System.out.println("sentenceTypeId:" + sentenceTypeId);
		System.out.println("keyword:" + keyword);
		System.out.println("text:" + text);

		Sentence sentence = new Sentence();
		SentenceType st = sentenceTypeService.getById(sentenceTypeId);
		sentence.setSentenceType(st);
		sentence.setText(text);
		sentence.setPaper(paperService.getById(paperId));
		Date today = new Date();
		String createdAt = DateUtils.date2Str(today, "-");
		sentence.setCreatedAt(createdAt);
		service.insertSentence(sentence);
		return "insertSentence";
	}

	public String searchSentence() {
		System.out.println("sentenceTypeId:" + sentenceTypeId);
		System.out.println("keyword:" + keyword);
		return "searchSentence";
	}

	// paper下所有的sentence列表
	public String paperSentence() {
		paperTitle = paperService.getById(paperId).getTitle();
		System.out.println("paper id:" + paperId);
		System.out.println("paper title:" + paperTitle);

		if (curPage == 0)
			curPage = 1;
		totalCount = (int) service.getTotalCountByPaperId(paperId);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		this.sentences = service.getByPaperId((curPage - 1) * pageSize, pageSize, paperId);
		System.out.println("sentence count:" + this.sentences.size());
		// load句子类别
		this.sentenceTypes = sentenceTypeService.getAll();
		return Action.SUCCESS;
	}

	public List<Sentence> getSentences() {
		return this.sentences;
	}

	public void prepare() throws Exception {
		// if (id != null)
		// employee = service.find(id);
	}

	public void setSentenceService(SentenceService service) {
		this.service = service;
	}

	public int getPaperId() {
		return paperId;
	}

	public void setPaperId(int paperId) {
		this.paperId = paperId;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public int getSentenceTypeId() {
		return sentenceTypeId;
	}

	public void setSentenceTypeId(int sentenceTypeId) {
		this.sentenceTypeId = sentenceTypeId;
	}

	public void setService(SentenceService service) {
		this.service = service;
	}

	public void setPaperService(PaperService paperService) {
		this.paperService = paperService;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setSentenceTypeService(SentenceTypeService sentenceTypeService) {
		this.sentenceTypeService = sentenceTypeService;
	}

	public void setSentences(List<Sentence> sentences) {
		this.sentences = sentences;
	}

	public List<SentenceType> getSentenceTypes() {
		return sentenceTypes;
	}

	public void setSentenceTypes(List<SentenceType> sentenceTypes) {
		this.sentenceTypes = sentenceTypes;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public String getPaperTitle() {
		return paperTitle;
	}

	public void setPaperTitle(String paperTitle) {
		this.paperTitle = paperTitle;
	}

}